import 'package:awesome_project/utils/utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:mpflutter_core/wechat_app_delegate.dart';
import 'package:mpflutter_wechat_api/mpflutter_wechat_api.dart';

class WxApiUtils {
  static void copyText(String content) {
    final option = SetClipboardDataOption()
      ..data = content
      ..success = (value) {
        Utils.showToast("复制成功");
      };
    wx.setClipboardData(option);
  }

  static void choose(Function(String path) doResult, {int count = 1}) {
    final option = ChooseMediaOption()
      ..success = (res) async {
        final tempMediaFile = res.tempFiles.first;
        doResult.call(tempMediaFile.tempFilePath);
      }
      ..fail = (err) {
        Utils.showToast("err.errMsg=${err.errMsg}");
      }
      ..count = count
      ..mediaType = ['image']
      ..sourceType = ['album', 'camera']
      ..sizeType = ['original', 'compressed'];
    wx.chooseMedia(option);
  }

  static void setAppShareInfo(BuildContext context,
      {String? title,
      String? imageUrl,
      String routeName = "/",
      String params = ""}) {
    MPFlutterWechatAppShareManager.setAppShareInfo(
      context: context,
      title: title ?? "",
      imageUrl: imageUrl,
      query: {
        'routeName': routeName,
        'params': params,
      },
    );
  }

  static void showShareMenu() {
    wx.showShareMenu(ShowShareMenuOption());
  }

  static void requestPayment() {
    wx.requestPayment(RequestPaymentOption());
  }

  static void requestPhone() {

  }

  static MPFlutterWechatAppDelegate initAppDelegate(BuildContext context,
      {Function(dynamic, dynamic)? onLaunchEnter}) {
    /**
     * 使用 AppDelegate 响应应用生命周期事件、分享事件。
     */
    return MPFlutterWechatAppDelegate(
      onLaunch: onLaunchEnter,
      onEnter: onLaunchEnter,
      onShow: () {
        print("onshow");
      },
      onHide: () {
        print("onhide");
      },
      onShareAppMessage: (detail) {
        return MPFlutterWechatAppShareManager.onShareAppMessage(detail);
      },
      onShareTimeline: (detail) {
        return MPFlutterWechatAppShareManager.onShareTimeline(detail);
      },
      onAddToFavorites: (detail) {
        return MPFlutterWechatAppShareManager.onAddToFavorites(detail);
      },
      onSaveExitState: () {
        return {
          "data": {"fooKey": "fooValue of exitState"},
          "expireTimeStamp": DateTime.now().millisecond + 24 * 60 * 60 * 1000
        };
      },
    );
  }
}
